package com.controller;



import com.Util.CheckToken;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.UserRole;
import com.entity.base.Result;
import com.service.UserRoleService;
import com.service.impl.UserServiceImpl;
import com.vo.TokenVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/userRole")
@Slf4j
@Api(tags = "角色权限相关接口", description = "提供相关的 Rest API")
public class UserRoleController extends BaseController<UserRole, UserRoleService>{

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    UserServiceImpl userService;

    @GetMapping("/getMenu")
    @ApiOperation(value = "获取不同用户的权限菜单")
    public Result getMenu(HttpServletRequest request) {
        //工具类验证token是否有效 有效返回tokenVo对象,否则返回null
        TokenVo tokenVo = new CheckToken().checkToken(request, userService);
        System.out.println(tokenVo);
        if (tokenVo != null) {//有效token

                String roleId = tokenVo.getRoleId();
                QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
                wrapper.eq("role_id", roleId);
                UserRole userRole = userRoleService.getOne(wrapper);
                return Result.success(userRole.getMenuInfo());

        } else {
            return Result.error("认证信息有误,获取数据失败");
        }
    }

}
